/**
 * $Id: OrganizationalUnit.java 6 2011-03-09 07:12:48Z r.middel@gmail.com $
 *
 * Copyright (C) 2010 mrBussy.eu
 * 
 * WickedHRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WickedHRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.mrbussy.applications.wickedhrm.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

/**
 * @author KLC31471
 *
 */
public class OrganizationalUnit {

	public static enum OrganizationUnitType { FUNCTIONAL, HIERARCHY }
	
	private OrganizationUnitType type = OrganizationUnitType.FUNCTIONAL;
	public OrganizationUnitType getType() { return this.type; }
	public void setType(OrganizationUnitType type) { this.type = type; }
	
	private Collection<OrganizationalUnit> parents;	
	public Collection<OrganizationalUnit> getParents() { return this.parents; }
	public void addParent(OrganizationalUnit parent) { 
		if((this.getType()==OrganizationUnitType.FUNCTIONAL)&&(this.parents.size()>=1))
			throw new IndexOutOfBoundsException("With a functional Organization Unit a maximum of one parent is allowed");
		
		this.parents.add(parent); 
	}
	
	private Collection<OrganizationalUnit> children;	
	public Collection<OrganizationalUnit> getChildren() { return this.children; }
	public void addChild(OrganizationalUnit child) { this.children.add(child); }
	
	private Organisation organisation;	
	public Organisation getOrganisation() { return this.organisation; }
	public void setOrganisation(Organisation organisation) { this.organisation = organisation; }

	private HashMap<String, Employee> employees;
	public HashMap<String, Employee> getEmployees() { return this.employees; }
	public void addEmployee(Employee emp) { this.employees.put(String.valueOf(emp.getEmployeeID()), emp); }
	
	private String name;
	public String getName() { return this.name;}
	public void setName(String name) { this.name = name; }
	
	private OrganizationalUnit(String name) {
		this.parents = new ArrayList<OrganizationalUnit>();
		this.children = new ArrayList<OrganizationalUnit>();
		this.employees = new HashMap<String, Employee>();
		this.setName(name);
	}
	private OrganizationalUnit(String name, OrganizationUnitType ofType) {
		this(name);
		this.setType(ofType);
	}
	public OrganizationalUnit(String name,OrganizationUnitType ofType, Organisation organisation) {
		this(name, ofType);
		this.setOrganisation(organisation);
	}
	public OrganizationalUnit(String name,OrganizationUnitType ofType, Organisation organisation, OrganizationalUnit parent) {
		this(name, ofType, organisation);
		this.addParent(parent);
	}
	
	public boolean hasChildren() {
		 return this.getChildren() == null ? false : this.getChildren().size()>0;
	}
	public boolean hasEmployees() {
		 return this.getEmployees() == null ? false : this.getEmployees().size()>0;
	}
	public boolean hasParents() {
		 return this.getParents() == null ? false : this.getParents().size()>0;
	}

	public String toString() { return this.getName(); }

}
